<?php
	require_once('environment.php');
	//Because of the lack of sense of sending an empty username and password to the server, we have this.
	if(!$acc->isLogged()){
		$validateLogin = "function validateLogin() {
			if(document.login.userName.value == \"\" || document.login.password.value == \"\"){
				document.getElementById(\"warn\").innerHTML = \"You must enter your username and password to log in!<br />\";
				return false;
			}
			return true;
		}";
	}
	
	$temp->currentPage("Home >> Account");
	
	//If the user isn't logged in we'll add the JS
	if(!$acc->isLogged()) $temp->addJS($validateLogin);
	
	//They have submenus if they are logged in!
	$tab = 8;
	if($acc->isLogged()) {
		$temp->addSubMenu("Create Account", "createAccount.php");
		$temp->addSubMenu("Change Password", "editAccount.php");
		$temp->addSubMenu("Delete Account", "deleteAccount.php");
		$temp->addSubMenu("Logout", "account.php?action=Logout");
		$tab = 9;
	}
	
	//The tab is set to the account tab here
	$temp->setTab($tab);
	$temp->generate();
	
	function throwErr($where, $err) {
		$err = urlencode($err);
		print change_site($where."?error=".$err);
	}
	
	if(!$acc->isLogged()) {
		//The first branch checks for the username in the POST data
		if(!fixArr($_POST, "userName")) {
			//If the account isn't logged in we'll give them this login form
			print "<form method=\"POST\" name=\"login\" action=\"\" onsubmit=\"return validateLogin();\">\n";
			print "<span id=\"warn\" style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />" : "")."</span>\n";
			print "<table width=20% align=center><tr><td><label>Username:</label</td></tr>";
			print "<tr><td><input type=\"text\" size=\"20\" maxlength=\"50\" name=\"userName\" /></td></tr>\n";
			print "<tr><td><label>Password:</label></td></tr>";
			print "<tr><td><input type=\"password\" size=\"20\" maxlength=\"50\" name=\"password\" /></td></tr>\n";
			print "<tr><td><input type=\"checkbox\" name=\"rememberMe\" />&nbsp;<label>Remember Me</label><br /></td></tr>\n";
			print "<tr></tr><tr></tr><td><input type=\"submit\" value=\"Login\" /> <input type=\"reset\" value=\"Clear\" /></td></tr>";
			print "<tr></tr><tr><td><a href=\"accRecovery.php\">I forgot my password</a></td></tr>";
			print "</table></form>\n";
		}else{ //The username was sent, try to log them in...
			$try = $acc->login(fixArr($_POST, "userName"), fixArr($_POST, "password"), fixArr($_POST, "rememberMe"));
			if(!$try){
				throwErr("account.php", "Your username or password was incorrect.");
			}else{
				print change_site("account.php");
			}
			die();
		}
	}else{
		//Otherwise, show them their stuff. They're logged in. 
		if(fixArr($_GET, "action") == "Logout") {
			$acc->logout();
			print change_site("account.php");
		}
		$username = $acc->username();
		print "Welcome to your account, {$username}!<br />\n";
		print "<a href=\"account.php?action=Logout\" style=\"text-decoration: underline;\">Logout</a>"; 
	}
?>